import os
import ckeynsecret

from django.utils import simplejson

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util

from myspace.myspaceapi import MySpace, MySpaceError
from oauthlib import oauth

class Test(webapp.RequestHandler):
  def __init__(self):
    webapp.RequestHandler.__init__(self)
    self.methods = TestMethods()

  def get(self):
    func = None
   
    action = self.request.get('action')
    if action:
      if action[0] == '_':
        self.error(403) # access denied
        return
      else:
        func = getattr(self.methods, action, None)
   
    if not func:
      self.error(404) # file not found
      return
     
    args = ()
    while True:
      key = 'arg%d' % len(args)
      val = self.request.get(key)
      
      if val:
        args += (simplejson.loads(val),)
      else:
        break
    result = func(*args)
    self.response.out.write(simplejson.dumps(result))

class TestMethods:
  def __init__(self):
    self.ms = MySpace(ckeynsecret.CONSUMER_KEY, ckeynsecret.CONSUMER_SECRET)
    self.ms.token = oauth.OAuthToken('', '')
    
  def Albums(self, *args):
    try:
        albums = self.ms.get_albums(args[0])
    except MySpaceError, mse:
        return "error"
    
    return albums

  def Album(self, *args):
    try:
        album = self.ms.get_album(args[0], args[1])
    except MySpaceError, mse:
        return "error"
    
    return album

  def Friends(self, *args):
    try:
        friends = self.ms.get_friends(args[0], 0, 20)
    except MySpaceError, mse:
        return "error"
    
    return friends

  def Friendship(self, *args):
    friends = ''
    friends += str(args[1])# + ';' + str(args[2])
    
    try:
        friendship = self.ms.get_friendship(args[0], friends)
    except MySpaceError, mse:
        return str(mse.http_response)
    
    return friendship

  def Mood(self, *args):
    try:
        mood = self.ms.get_mood(args[0])
    except MySpaceError, mse:
        return "error"
    
    return mood

  def Moods(self, *args):
    try:
        moods = self.ms.get_moods(args[0])
    except MySpaceError, mse:
        return "error"
    
    return moods

  def Photos(self, *args):
    try:
        photos = self.ms.get_photos(args[0])
    except MySpaceError, mse:
        return "error"
    
    return photos

  def Photo(self, *args):
    try:
        photo = self.ms.get_photo(args[0], args[1])
    except MySpaceError, mse:
        return "error"
    
    return photo

  def Profile(self, *args):
    try:
        profile = self.ms.get_profile(args[0], 'extended')
    except MySpaceError, mse:
        return "error"
    
    return profile

  def Status(self, *args):
    try:
        status = self.ms.get_status(args[0])
    except MySpaceError, mse:
        return "error"
    
    return status

  def Videos(self, *args):
    try:
        videos = self.ms.get_videos(args[0])
    except MySpaceError, mse:
        return "error"
    
    return videos

  def Video(self, *args):
    try:
        video = self.ms.get_video(args[0], args[1])
    except MySpaceError, mse:
        return "error"
    
    return video

  def Activities(self, *args):
    try:
        activities = self.ms.get_activities_atom(args[0])
    except MySpaceError, mse:
        return "error"
    
    return activities

  def ActivitiesFriends(self, *args):
    try:
        activitiesFriends = self.ms.get_friends_activities_atom(args[0])
    except MySpaceError, mse:
        return "error"
    
    return activitiesFriends

  def SetStatus(self, *args):
    try:
        setStatus = self.ms.set_status(args[0], args[1])
    except MySpaceError, mse:
        return "error"
    
    return setStatus

  def SetMood(self, *args):
    try:
        setMood = self.ms.set_mood(args[0], args[1])
    except MySpaceError, mse:
        return "error"
    
    return setMood

  def CreateAlbum(self, *args):
    try:
        createAlbum = self.ms.create_album(args[0], args[1], args[2], args[3])
    except MySpaceError, mse:
        return "error"
    
    return createAlbum

  def Indicators(self, *args):
    try:
        indicators = self.ms.get_indicators(args[0])
    except MySpaceError, mse:
        return "error"
    
    return indicators

  def SendNotification(self, *args):
    try:
        sendNotification = self.ms.send_notification(args[1], args[2], args[3], args[4], args[5], args[6], args[7], args[8])

    except MySpaceError, mse:
        return "error"
    
    return sendNotification

def main():
  application = webapp.WSGIApplication([
    ('/test', Test),
    ], debug=True)
  util.run_wsgi_app(application)

if __name__ == '__main__':
  main()